const fs = require("fs");
const path = require("path");
const http = require("http");

let app = http.createServer(async (req, res) => {
  res.setHeader("Content-Type", "text/html;charset=utf-8"); 
  let urls = req.url;

  if (urls == "/index.html" || urls == "/") {
   let data = await readFileFn('./index.html')
   res.write(data)
  } else if (urls == "/news.html") {
    let data = await readFileFn('./news.html')
    res.write(data)
  } else if  (urls == "/list.html") {
    let data = await readFileFn('./list.html')
    res.write(data)
  } else {
    res.write("404");
  }
  res.end();
});

app.listen(3000, () => {
  console.log("3000 run");
});

let readFileFn = (urls)=> {
    //处理文件地址
    let pathurl = path.join(__dirname, urls)
    return new Promise((resolve,reject)=> {
        fs.readFile(pathurl,'utf8',(err,data)=> {
            if(err) {
                reject(err)
            } else {
                resolve(data)
            }
        })
    })
}